﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Text;

namespace BGShell
{
    public abstract class BGShellHelpCategory : BGShellHelpTopic
    {
        public abstract ICollection<BGShellHelpTopic> Topics
        {
            get;
        }

        public abstract ICollection<BGShellHelpCategory> Categories
        {
            get;
        }

        public override void BeginWriteHtml(IBGShellHelpContext context)
        {
            var result = new StringBuilder();

            result.Append("<h3>");
            result.Append(Title);
            result.Append("</h3>");

            result.Append("<ul>");

            foreach (var topic in Topics)
            {
                result.Append("<li>");
                result.Append(MakeLink(topic.Title));
                result.Append("</li>");
            }

            foreach (var cat in Categories)
            {
                result.Append("<li>");
                result.Append(MakeLink(cat.Title));
                result.Append("</li>");
            }

            result.Append("</ul>");

            context.WriteHtml(result.ToString());
            context.Done();
        }

        public override BGShellHelpTopic Navigate(string topic)
        {
            foreach (var t in Topics)
            {
                if (t.Title == topic)
                {
                    return t;
                }
            }

            foreach (var c in Categories)
            {
                if (c.Title == topic)
                {
                    return c;
                }
            }

            return base.Navigate(topic);
        }

        private static string MakeLink(string title)
        {
            return string.Format(CultureInfo.InvariantCulture, "<a href=\"{0}\">{0}</a>", title);
        }
    }
}
